package com.bloveoacom.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bloveoacom.weichi.R;


public class MinusAndPlusBtn extends RelativeLayout {
    private View root_view;
    private ImageView iv_mminus, iv_plus;
    private TextView tv_goods_number;
    private int number = 1;
    private int maxNumber=2;
    private MinusAndPlusListener minusAndPlusListener;
    public MinusAndPlusBtn(Context context, AttributeSet attrs) {
        super(context, attrs);
        root_view = inflate(context, R.layout.layout_minus_and_plus_btn, null);
        iv_mminus = (ImageView) root_view.findViewById(R.id.iv_minus);
        iv_plus = (ImageView) root_view.findViewById(R.id.iv_plus);
        tv_goods_number = (TextView) root_view.findViewById(R.id.tv_goods_num);
        addView(root_view);
        iv_mminus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                minus();
            }
        });
        iv_plus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                plus();
            }
        });



    }

    public void setMinusAndPlusListener(MinusAndPlusListener minusAndPlusListener) {
        this.minusAndPlusListener = minusAndPlusListener;
    }

    public int getNumber() {
        return number;
    }
    public void init(int number) {
        this.number = number;
        tv_goods_number.setText(String.valueOf(number));

    }

    public void minus() {
        if (number > 1) {
            number--;
            tv_goods_number.setText(String.valueOf(number));
            if(minusAndPlusListener!=null){
                minusAndPlusListener.minusAndPlusClickListener(number);
            }
        }
    }

    public void plus() {
        if(number<maxNumber){
            number++;
            tv_goods_number.setText(String.valueOf(number));
            if(minusAndPlusListener!=null){
                minusAndPlusListener.minusAndPlusClickListener(number);
            }
        }


    }

   public interface  MinusAndPlusListener{
        void minusAndPlusClickListener(int number);
    }

}
